IDBans = {};
IPBans = {};
DomainBans = {};

Script.LoadScript("bans/ID Bans.lua");
Script.LoadScript("bans/IP Bans.lua");
Script.LoadScript("bans/Domain Bans.lua");

function XWriteIDBanToFile()
	FileHnd, Err = io.open("Game/bans/ID Bans.lua","w");

	if (not FileHnd) then
		return;
	end;

    FileHnd:write("IDBans = {\n");
    for i,id in pairs(IDBans) do
        id = tostring(id);
        FileHnd:write('"'..id..'",\n');
    end
    FileHnd:write("};");
    FileHnd:close();
end

function XWriteIPBanToFile()
	FileHnd, Err = io.open("Game/bans/IP Bans.lua","w");

	if (not FileHnd) then
		return;
	end;

    FileHnd:write("IPBans = {\n");
    for i,ip in pairs(IPBans) do
        ip = tostring(ip);
        if (ip and ip~=0) then
            FileHnd:write('"'..ip..'",\n');
        end
    end
    FileHnd:write("};");
    FileHnd:close();
end

function XWriteDomainBanToFile()
	FileHnd, Err = io.open("Game/bans/Domain Bans.lua","w");

	if (not FileHnd) then
		return;
	end;

    FileHnd:write("DomainBans = {\n");
    for i,domain in pairs(DomainBans) do
        domain = tostring(domain);
        FileHnd:write('"'..domain..'",\n');
    end
    FileHnd:write("};");
    FileHnd:close();
end

--------------------------------------------------------------------------------

function XWriteAllBans()
    if (XCfgVar.useIDBans) then
            XWriteIDBanToFile();
    end
    if (XCfgVar.useIPBans) then
        XWriteIPBanToFile();
    end
    if (XCfgVar.useDomainBans) then       
        XWriteDomainBanToFile();
    end
end

System.AddCCommand("x_writebans",					"XWriteAllBans()",				"Writes Bans to lua file");


function XPermaBan(player, reason)
    if (not player) then
        return;
    end
    if (XCfgVar.useIDBans) then
        local ProfID = player.info.profile;
        table.insert(IDBans, ProfID);
    end
    if (XCfgVar.useIPBans) then
	   local IP = player.info.ip;
	   table.insert(IPBans, IP);
	end
	if (XCfgVar.useDomainBans) then
	   local domain = player.info.host;
	   table.insert(DomainBans, host);
	end
	
    XWriteAllBans();
    LogAdmins("[AEGIS] $4"..player:GetName().." was permabanned for "..reason);
end
	

System.AddCCommand("x_permaban",        "XPermaBan, %1, %2",     "Permabans a player");
	

function XIsBanned(player)
        if(XCfgVar.useIDBans) then
            if (IDBans) then
                player.banHimId = false;
                for i,ID in pairs(IDBans) do
                    if (player.info.profile==ID) then
                        player.banHimId = true;
                    end
                end
            end
        end
        
        if(XCfgVar.useIPBans) then
            if (IPBans) then
                player.banHimIp = false;
                for i,IP in pairs(IPBans) do
                    if (player.info.ip==IP) then
                        player.banHimIp = true;
                    end
                end
            end
        end
        
        if(XCfgVar.useDomainBans) then
            if (DomainBans) then
                player.banHimDomain = false;
                for i,Domain in pairs(DomainBans) do
                    if (player.info.host==Domain) then
                        player.banHimDomain = true;
                    end
                end
            end
        end  
        
        if (player.banHimId) then
            player.banHimId = nil;
            player.banHimIp = nil;
            player.banHimDomain = nil;
            if (XCfgVar.notifyBanCheck) then
                XSendConsoleMessageToPlayer(player, "[AEGIS] $4 FAILED ID Ban cross-check");
            end
            LogAlways("[AEGIS] Player "..player:GetName().." failed ID Ban cross-check!");
            CryAction.BanPlayer(player.id, "[AEGIS] ID PERMABAN");
            return;
        else
            if (XCfgVar.notifyBanCheck) then
                XSendConsoleMessageToPlayer(player, "[AEGIS] $3 Passed ID Ban cross-check");
            end
            LogAlways("[AEGIS] Player "..player:GetName().." passed ID Ban cross-check!");
        end
        
        if (player.banHimIp) then
            player.banHimId = nil;
            player.banHimIp = nil;
            player.banHimDomain = nil;
            if (XCfgVar.notifyBanCheck) then
                XSendConsoleMessageToPlayer(player, "[AEGIS] $4 FAILED IP Ban cross-check");
            end
            LogAlways("[AEGIS] Player "..player:GetName().." failed IP Ban cross-check!");
            CryAction.BanPlayer(player.id, "[AEGIS] IP PERMABAN");
            return;
       else
            if (XCfgVar.notifyBanCheck) then
                XSendConsoleMessageToPlayer(player, "[AEGIS] $3 Passed IP Ban cross-check");
            end
            LogAlways("[AEGIS] Player "..player:GetName().." passed IP Ban cross-check!");
        end

        if (player.banHimDomain) then
            player.banHimId = nil;
            player.banHimIp = nil;
            player.banHimDomain = nil;
            if (XCfgVar.notifyBanCheck) then
                XSendConsoleMessageToPlayer(player, "[AEGIS] $4 FAILED Domain Ban cross-check");
            end
            LogAlways("[AEGIS] Player "..player:GetName().." failed Domain Ban cross-check!");
            CryAction.BanPlayer(player.id, "[AEGIS] DOMAIN PERMABAN");
       else
            if (XCfgVar.notifyBanCheck) then
                XSendConsoleMessageToPlayer(player, "[AEGIS] $3 Passed Domain Ban cross-check");
            end
            LogAlways("[AEGIS] Player "..player:GetName().." passed Domain Ban cross-check!");
        end
            
end
